unit UDM;

interface

uses
  SysUtils, Classes, DB, ADODB;

type
  TDMPrincipal = class(TDataModule)
    ADOQryPrincipal: TADOQuery;
    ADOConnection1: TADOConnection;
    ADOConnection2: TADOConnection;
    //procedure DataModuleCreate(Sender: TObject);
  private
    { Private declarations }
    FStringConexao: String;
    FADOQry: TADOQuery;
    function GetStringConexao: String;
    procedure SetStringConexao(const Value: String);
  public
    { Public declarations }
    constructor Create;
    function TestaConexaoDB: Boolean;
    function ValidaUsuario: Boolean;
    function GetDMADOQuery: TADOQuery;
    procedure SetDMADOQuery(const Value: TADOQuery);
  end;

var
  DMPrincipal: TDMPrincipal;

implementation

uses Dialogs;

{$R *.dfm}

{ TDMPrincipal }

constructor TDMPrincipal.Create;
begin
  FStringConexao := ADOConnection1.ConnectionString;
end;

function TDMPrincipal.GetDMADOQuery: TADOQuery;
begin
  Result := FADOQry;
end;

function TDMPrincipal.GetStringConexao: String;
begin
  Result := FStringConexao;
end;

procedure TDMPrincipal.SetDMADOQuery(const Value: TADOQuery);
begin
  FADOQry := Value;
end;

procedure TDMPrincipal.SetStringConexao(const Value: String);
begin
  FStringConexao := Value;
end;

function TDMPrincipal.TestaConexaoDB: Boolean;
  var
    ADOQryTestConn: TADOQuery;
begin
  try
    try
      ADOQryTestConn := TADOQuery.Create(Self);
      ADOQryTestConn.Connection := ADOConnection1;
      ADOQryTestConn.Close;
      ADOQryTestConn.SQL.Clear;
      ADOQryTestConn.SQL.Text := 'SELECT USU_ID FROM USUARIO WHERE USU_ID <= 5';
      ADOQryTestConn.Open;

      if(ADOQryTestConn.RecordCount > 0)then
      begin
        Result := True;
      end
      else
      begin
        Result := False;
      end;
    except
      On Erro: Exception do
      begin
        ShowMessage('ERRO : [TDMPrincipal].[TestConnection] ' + #13 + #13 + 'Msg.: ' + Erro.Message);
      end;
    end; // fim try except
  finally
    ADOQryTestConn.Free;
  end; // fim try finally
end;

function TDMPrincipal.ValidaUsuario: Boolean;
  var
    ADOQryMain: TADOQuery;
begin
  try
    if(Not ADOConnection1.Connected)then
    begin
      ADOConnection1.Connected := True;
    end;

    try
      ADOQryMain := TADOQuery.Create(Self);
      ADOQryMain.Connection := ADOConnection1;
      ADOQryMain.Close;
      ADOQryMain.SQL.Clear;

      ADOQryMain.SQL.Text := 'SELECT UPPER(usu_login), usu_senha FROM USUARIO';
      //ADOQryMain.Parameters.ParamByName('USERX').Value := Self.GetUsuario;
      //ADOQryMain.Parameters.ParamByName('PWDX').Value := Self.GetSenha;
      ADOQryMain.Open;

      if(ADOQryMain.RecordCount > 0)then
      begin
        Self.SetDMADOQuery(ADOQryMain);
        {*
        while(not ADOQryMain.Eof)do
        begin
          Self.SetApelido(ADOQryMain.FieldByName('usu_login').AsString);
          ADOQryMain.Next;
        end; // fim while
        *}

        Result := True;
      end
      else
      begin
        Result := False;
      end;
    except
      On Erro: Exception do
      begin
        ShowMessage('ERRO : [TDMPrincipal].[TestConnection] ' + #13 + #13 + 'Msg.: ' + Erro.Message);
      end;
    end; // fim try except
  finally
    ADOQryMain.Free;
  end; // fim try finally
end;



end.
